import { PrismaClient } from '@prisma/client'
import { hash } from 'bcryptjs'

const prisma = new PrismaClient()

async function main() {
  // 清理现有数据
  await prisma.session.deleteMany()
  await prisma.notification.deleteMany()
  await prisma.membership.deleteMany()
  await prisma.order.deleteMany()
  await prisma.operationLog.deleteMany()
  await prisma.user.deleteMany()

  console.log('Seeding...')

  // 创建管理员用户
  const adminUser = await prisma.user.create({
    data: {
      username: 'admin',
      email: 'admin@example.com',
      password: await hash('admin123', 12),
      name: '管理员',
      isActive: true,
      loginCount: 0,
      licenseNumber: 'ADMIN001',
      lawFirm: '示例律师事务所',
      practiceArea: '综合管理',
    },
  })

  // 创建普通用户
  const normalUser = await prisma.user.create({
    data: {
      username: 'user',
      email: 'user@example.com',
      password: await hash('user123', 12),
      name: '测试用户',
      isActive: true,
      loginCount: 0,
      licenseNumber: 'TEST001',
      lawFirm: '测试律师事务所',
      practiceArea: '民商事诉讼',
    },
  })

  // 为普通用户创建会员信息
  await prisma.membership.create({
    data: {
      userId: normalUser.id,
      type: 'basic',
      startDate: new Date(),
      endDate: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000), // 30天后
      autoRenew: false,
      status: 'active',
    },
  })

  // 创建一些测试通知
  await prisma.notification.createMany({
    data: [
      {
        userId: adminUser.id,
        type: 'system',
        title: '欢迎使用管理员账号',
        content: '您已成功登录管理员账号，请及时修改默认密码。',
      },
      {
        userId: normalUser.id,
        type: 'system',
        title: '欢迎使用律师AI助手',
        content: '感谢您注册使用我们的服务，请随时联系我们获取帮助。',
      },
    ],
  })

  console.log('Seeding finished.')
}

main()
  .catch((e) => {
    console.error(e)
    process.exit(1)
  })
  .finally(async () => {
    await prisma.$disconnect()
  })
